package com.hw.backapi.service.impl;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.backapi.service.IClientService;
import com.hw.model.entity.ClientPO;
import com.hw.model.mapper.ClientMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 * @author lipeng
 * @description 前端客户端
 * @date 2023/3/6 16:00
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientPO> implements IClientService {

    /**
     * @descrition 创建用户
     * @author lipeng
     * @date 2023/3/6
     * @param
     * @return boolean
     */
    @Override
    public boolean createClient(String phone, String password, Integer isDoctor){
        ClientPO clientPO = new ClientPO();
        clientPO.setCellphone(phone);
        clientPO.setPassword(generatePassword(password));
        clientPO.setLastLoginIp("");
        clientPO.setLastLoginTime(new Date());
        clientPO.setStatus(1);
        clientPO.setCsvCount(0);
        clientPO.setToken("");
        clientPO.setIsDoctor(isDoctor);
        clientPO.setDownable(0);
        clientPO.setCreatedAt(new Date());
        clientPO.setUpdatedAt(new Date());

        return this.save(clientPO);
    }

    @Override
    public List<ClientPO> getClientInfo(List<String> phones) {
        if(CollectionUtil.isEmpty(phones)){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ClientPO> query = new LambdaQueryWrapper<>();
        query.in(ClientPO::getCellphone,phones);
        return baseMapper.selectList(query);
    }

    /**
     * @descrition 生成密码
     * @author lipeng
     * @date 2023/3/6
     * @param password 密码
     * @return java.lang.String
     */
    private String generatePassword(String password){
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    /**
     * @descrition 校验密码是否正确
     * @author lipeng
     * @date 2023/3/6
     * @param password 原密码
     * @param encodedPassword 加密过后的密码
     * @return java.lang.Boolean
     */
    private boolean checkPassword(String password,String encodedPassword){
        return BCrypt.checkpw(password, encodedPassword);
    }
}
